<template>
  <div style='height: 100%'>
    <el-breadcrumb separator-class='el-icon-arrow-right'>
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>学生管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!--卡片试图区域-->
    <el-card style='height: 95%'>
      <el-row :gutter='20'>
        <el-col :span='5'>
          <el-input placeholder='请输入内容' v-model='queryInfo.query'
                    clearable @clear='getUserList'>
            <el-button slot='append' icon='el-icon-search' @click='getUserList'></el-button>
          </el-input>
        </el-col>
        <el-col :span='4'>
          <el-button type='primary' @click='dialogFormVisible = true'>
            添加用户
          </el-button>
        </el-col>
      </el-row>

      <el-dialog title="添加学生信息" :visible.sync="dialogFormVisible">
        <student-form v-on:msg="getMsg"></student-form>
      </el-dialog>

      <el-table :data='userList' border stripe height='600px'>
        <el-table-column type='index' label='编号' />
        <el-table-column label='姓名' prop='studentName' />
        <el-table-column label='学号' prop='studentID' />
        <el-table-column label='性别' prop='gender' />
        <el-table-column label='年级' prop='grade' />
        <el-table-column label='班级' prop='className' />
        <el-table-column label='电话' prop='telephone' />
        <el-table-column label='宿舍号' prop='addr' />
        <el-table-column label='操作'>
          <template slot-scope='scope'>
            <el-button type='primary' icon='el-icon-edit' size='mini'></el-button>
            <el-button type='danger' icon='el-icon-delete' size='mini'></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--分页区域-->
      <el-pagination
        @size-change='handleSizeChange'
        @current-change='handleCurrentChange'
        :current-page='queryInfo.pageNum'
        :page-sizes='[10, 20, 50, 100]'
        :page-size='queryInfo.pageSize'
        layout='sizes, prev, pager, next, jumper'
        :total='this.total'
        style='float: right'>
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import StudentForm from '@/components/student/studentForm'
export default {
  name: 'Student',
  components: { StudentForm },
  data() {
    return {
      queryInfo: {
        query: '',
        pageNum: 1,
        pageSize: 10
      },
      userList: [],
      total: 0,
      student: {
        addr: '',
        className: '',
        grade: '',
        gender: '',
        studentID: '',
        studentName: '',
        telephone: ''
      },
      dialogFormVisible: false
    }
  },
  created() {
    this.getUserList()
  },
  methods: {
    async getUserList() {
      const { data: res } = await this.$http.get('/api/student/getPage', { params: this.queryInfo })
      if (res.code !== 0) {
        return this.$message.error('获取用户失败')
      }
      this.userList = res.data.records
      this.total = res.data.total
    },
    // 监听pagesize改变事件
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize
      this.getUserList()
    },
    // 页码值改变事件
    handleCurrentChange(newNum) {
      this.queryInfo.pagenum = newNum
      this.getUserList()
    },
    getMsg: function(val) {
      this.dialogFormVisible = false
    }
  }
}
</script>

<style scoped>

</style>
